package com.hrpmar.controller;

import com.hrpmar.pojo.*;
import com.hrpmar.service.DrugstockService;
import com.hrpmar.service.PurplanService;
import com.hrpmar.utils.genNumUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 李松松 on 2017/11/21.
 */
@Controller
@RequestMapping("stock")
public class DrugstockController {

    @Resource
    private DrugstockService drugstockService;
    @Resource
    private PurplanService purplanService;

    @GetMapping("correctionDrugstock")
    public String correctionDrugstock(String invNum,Model model){
        Integer i = drugstockService.correctionTakesto(invNum);
        String msg = (i>0?"操作成功!":"操作失败!");
        model.addAttribute("msg",msg);
        return "stock/takestoView";
    }


    @GetMapping("takestoDetails")
    public String takestoDetails(String invNum,Model model){
        Inventorying inventorying = drugstockService.searchDan(invNum);//获取主表信息
        List<Invdetails> list = drugstockService.searchByInventorying(invNum);
        model.addAttribute("inventorying",inventorying);
        model.addAttribute("list",list);
        return "stock/takestoDetails";
    }

    @PostMapping("addTakestoDrugstock")
    public String addTakestoDrugstock(Inventorying inventorying,Invdetails invdetails,HttpServletRequest req){
        Integer i = drugstockService.addTakestoDrugstockAspect(inventorying,invdetails,req);
        String msg = (i>0?"添加成功!":"添加失败!");
        System.out.println(msg);
        return "stock/takestoView";
    }

    @GetMapping("/SeeDestory")
    public String SeeDestory(String desNum,Model model){
        List<Destorydetails> list = drugstockService.searchDestoryDetailsSee(desNum);
        model.addAttribute("list",list);
        return "stock/SeeDestory";
    }

    @GetMapping("/addDestock")
    public String addDestory(Destorymaster destorymaster, String[] stoNum,Model model){
        Integer i = drugstockService.addDestoryMasterAspect(destorymaster,stoNum);
        String msg = (i>0?"提交审核成功!":"提交审核失败!");
        model.addAttribute("msg",msg);
        return "stock/destoryView";
    }

    //销毁页面
    @RequestMapping("destroy")
    public String Destroy(String[] stoNum,Model model){
        List<Drugstock> list = drugstockService.searchDestoryDruginfo(stoNum);
        double money = 0d;
        for (Drugstock d:list){
            money+=d.getStoPrimecost()*d.getStoPcs();
        }
        model.addAttribute("money",money);
        model.addAttribute("list",list);
        return "stock/Destroy";
    }

    @GetMapping("batchAddStock")
    public String batchAddStock(Integer bh,String[] stoNum,Model model,HttpServletRequest req){
        Map<String,Purdetails> map = drugstockService.searchDan(stoNum,bh,req);
        model.addAttribute("purdetailsW",map);
        return "pur/draw";
    }

    @GetMapping("addStock")
    public String addStock(Drugstock drugstock, Purdetails purdetails,String drugName,Model model,HttpServletRequest req){
        purdetails.setDelQoh(drugstock.getStoPcs());//现有数量
        purdetails.setDelOneprice(drugstock.getStoPresell());//零售价格
        purdetails.setDelBuyprice(drugstock.getStoPrimecost());//购进价格
        purdetails.setDelRetailprice(drugstock.getStoPrimecost()-2);//批发价格
        Druginfo druginfo = new Druginfo();//药品信息对象
        druginfo.setDrugName(drugName);//药品名称
        purdetails.setDruginfo(druginfo);
        Map<String,Purdetails> map = new HashMap<>();
        String planNum = genNumUtil.getNum(purplanService.searchNewPurNum());//获取采购主表编号
        purdetails.setPlanNum(planNum);//采购详情添加采购主表编号
        String keyId = purplanService.addDels2Map(purdetails,req);//添加到Map中
        map.put(keyId,purdetails);
        model.addAttribute("purdetailsW",map);
        return "pur/draw";
    }




    @GetMapping("display")
    public String display(String drugNum, HttpServletRequest request){
        request.setAttribute("drugNum",drugNum);
        return "stock/display";
    }
    @GetMapping("drugDisplay")
    public String drugDisplay(String drugNum, Model model){
        Druginfo druginfo = drugstockService.searchByNum(drugNum);
        model.addAttribute("druginfo",druginfo);
        return "stock/eawarn";
    }
}
